package hk.meiyan.com.kotlincode

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import hk.meiyan.com.kotlincode.okhttp.OkCallBack
import hk.meiyan.com.kotlincode.okhttp.OkhttpUtils
import hk.meiyan.com.kotlincode.young.YCallBack
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.Call
import okhttp3.Response
import java.io.IOException

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textview.setText("我很有自知之明的")
        YCallBack.getIndance(textview).onFailure()

    }
    // 网络去请求
    fun request() {
        OkhttpUtils.getIndance().get(
                "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&ch=4&tn=56060048_4_pg&wd=kotlin%20%E6%9C%89%E4%B8%AA%E5%8F%82%E6%95%B0%E6%98%AF%E6%8E%A5%E5%8F%A3&oq=kotlin%25E6%2580%258E%25E4%25B9%2588new%25E4%25B8%2580%25E4%25B8%25AA%25E6%258E%25A5%25E5%258F%25A3&rsv_pq=e0b3b4c400046e99&rsv_t=0ea6qkmSdvHbt3n9CLP9NBJjkTCsKTqU%2Bjw2waO2hpPQyt%2FBl1AEQT6%2FHUFTV1Uzn2PR5g&rqlang=cn&rsv_enter=1&rsv_sug3=30&rsv_sug1=33&rsv_sug7=100&rsv_sug2=0&inputT=18040&rsv_sug4=18694",
                object : OkCallBack {
                    override fun onFailure() {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                    override fun onFailure(call: Call?, e: IOException?) {
                        runOnUiThread {
                            textview.setText("请求失败")
                        }
                    }

                    override fun onResponse(call: Call?, response: Response?) {
                        runOnUiThread {
                            textview.setText("请求成功")
                        }
                    }
                }
        )
    }

}
